# file containing all tower of daegeros code not imported from libtcod
import libtcodpy as libtcod

import backend
import data
import draw
import input


#
# INITIALIZE! #
#

libtcod.console_set_custom_font(
    'courier12x12_aa_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)


libtcod.console_init_root(data.SCREEN_WIDTH, data.SCREEN_HEIGHT, 'Tower of Daegeros')
libtcod.sys_set_fps(data.LIMIT_FPS)
libtcod.console_set_background_color(0, libtcod.black)
CONSOLE = libtcod.console_new(data.MAP_WIDTH, data.MAP_HEIGHT)
PANEL = libtcod.console_new(data.SCREEN_WIDTH, data.PANEL_HEIGHT)
LOADOUT = libtcod.console_new(data.LOADOUT_WIDTH, data.LOADOUT_HEIGHT)
MESSAGES = []  # list of messages to be displayed
old_messages = [] # messages no longer being displayed
WORLD = {}  # dictionary of scene.name:scene pairs
fov_redraw = True
TIME_PASSED = 0

# create the player
player = backend.Player(stats = {'strength': 5, 'stamina': 5, 'focus': 5})

# create the first scene
first_scene = backend.map_gen('Lobby')
current_scene = first_scene
WORLD[first_scene.name] = first_scene
current_scene.spawn(unit=player)
for _ in xrange(5):
    current_scene.spawn(name='alex')

current_scene.forge(name='stick',location=player.location)
current_scene.forge(name='stick',location=current_scene.random_floor_tile())

welcome_msg = ['Welcome to the Tower of Daegeros!', 'Have a nice death!']
draw.draw_stuff(CONSOLE, PANEL, LOADOUT, current_scene, player, welcome_msg, MESSAGES, old_messages, fov_redraw, TIME_PASSED)
#
# MAIN LOOP! #
#

while player.alive and not libtcod.console_is_window_closed():
    player.update_bonus_skills()
    while player.WAIT < 1:
        # handle player actions and draw screen
        fov_redraw, output = input.get_input(current_scene, player, CONSOLE)
        fov_redraw = draw.draw_stuff(CONSOLE, PANEL, LOADOUT, current_scene, player, 
                                     output, MESSAGES, old_messages, fov_redraw, TIME_PASSED)
    while player.WAIT > 0:
        # handle and draw other units' actions
        fov_redraw, output = backend.update_game_state(current_scene, player)
        fov_redraw = draw.draw_stuff(CONSOLE, PANEL, LOADOUT, current_scene, player, 
                                     output, MESSAGES, old_messages, fov_redraw, TIME_PASSED)
    TIME_PASSED += 1
